﻿namespace TestTool.Tests.Common.Discovery
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using TestTool.Proxies.WSDiscovery;
    using TestTool.Tests.Common.Soap;

    public class DiscoveryHeaderBuilder : ISoapHeaderBuilder
    {
        private const string _helloAction = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Hello";
        private const string _probeAction = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";
        private const string _wsa = "http://schemas.xmlsoap.org/ws/2004/08/addressing";

        protected string GetAction(object message)
        {
            string str = string.Empty;
            if (message is ProbeType)
            {
                return "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";
            }
            if (message is HelloType)
            {
                str = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Hello";
            }
            return str;
        }

        public void WriteHeader(XmlWriter writer, object message)
        {
            writer.WriteElementString("wsa", "MessageID", "http://schemas.xmlsoap.org/ws/2004/08/addressing", "uuid:" + Guid.NewGuid().ToString());
            writer.WriteElementString("wsa", "To", "http://schemas.xmlsoap.org/ws/2004/08/addressing", "urn:schemas-xmlsoap-org:ws:2005:04:discovery");
            writer.WriteElementString("wsa", "Action", "http://schemas.xmlsoap.org/ws/2004/08/addressing", this.GetAction(message));
            if (!string.IsNullOrEmpty(this.OrigingMessageId))
            {
                writer.WriteElementString("wsa", "RelatesTo", "http://schemas.xmlsoap.org/ws/2004/08/addressing", this.OrigingMessageId);
            }
        }

        public string OrigingMessageId { get; set; }
    }
}

